package com.hippo.javacore.multithreading;

/**
 * @ClassName ThreadDemo1
 * @Description TODO 继承Thread类重写run()方法
 * @Author tangxl
 * @create 2023-03-10 10:55
 **/
public class ThreadDemo1 extends Thread {
    private String name;
    public ThreadDemo1(String name){
        System.out.println("创建线程:"+name);
    }


    /**
     * 重写Thread的run()方法
     */
    @Override
    public void run() {
        // 循环输出1~随机数
        int randomNumber = (int)(Math.random() * 100) + 1;
        for (int i = 0; i < randomNumber; i++) {
            System.out.println(getName()+"输出"+i);
        }
    }

    public static void main(String[] args) {
        /**
         * 继承Thread类重写run()方法创建多线程：
         * 1.自己定义一个类继承Thread
         * 2.重写run()方法
         * 3.创建子类线程，并启动线程
         */
        // 继承Thread创建线程
        Thread thread1 = new ThreadDemo1("线程A");
        Thread thread2 = new ThreadDemo1("线程B");
        // 线程命名
        thread1.setName("线程A");
        thread2.setName("线程B");
        // 就绪状态
        thread1.start();
        thread2.start();
    }
}
